﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SalesSys.DAL.Base;
using SalesSys.Comm.DAL;
using SalesSys.Comm.Utility;

namespace SalesSys.DAL.Models
{
    /*
     * 供应商相关内容实体
     */
    /// <summary>
    /// 钢厂品牌
    /// </summary>
    public class Supplier : DbEntityBase<Guid>
    {

        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 对外品牌名称
        /// </summary>
        public string Brand { get; set; }

        /// <summary>
        /// 地区
        /// </summary>
        public virtual District District { get; set; }

        /// <summary>
        /// 地址
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// 联系人
        /// </summary>
        public string Contacts { get; set; }

        /// <summary>
        /// 联系人手机号
        /// </summary>
        public string Phone { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Remarks { get; set; }

        /// <summary>
        /// 商品列表
        /// </summary>
        public virtual ICollection<Commodity> Commodities { get; set; }

        /// <summary>
        /// 基准价
        /// </summary>
        public decimal BasePrice { get; set; }
        public class Mapper : SalMapper<Supplier, Guid>
        {
            public Mapper()
            {
                Property(p => p.Name).HasMaxLength(50);
                HasOptional(p => p.District);
                Property(p => p.Address).HasMaxLength(255);
                Property(p => p.Contacts).HasMaxLength(50);
                Property(p => p.Phone).HasMaxLength(11);
            }
        }

        /// <summary>
        /// 钢厂品牌转供应商实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Provider ToProvider(Provider entity = null)
        {
            if (entity == null)
            {
                entity = new Provider();
            }
            entity.Address = this.Address;
            entity.District = this.District;
            entity.Name = this.Name;
            entity.ProviderType = ProviderType.Producer;
            entity.Supplier = this;
            return entity;
        }
    }



   

    /// <summary>
    /// 仓库商品,DepotId,CommodityId
    /// </summary>
    public class DepotCommodity : IDbEntity
    {
        /// <summary>
        /// 是否是基准价
        /// </summary>
        public bool IsBasePrice { get; set; }

        /// <summary>
        /// 供应商
        /// </summary>
        public virtual Provider Provider { get; set; }

        /// <summary>
        /// 供应商编号
        /// </summary>
        public Guid ProviderId { get; set; }

        /// <summary>
        /// 商品
        /// </summary>
        public virtual Commodity Commodity { get; set; }

        /// <summary>
        /// 商品编号
        /// </summary>
        public Guid CommodityId { get; set; }

        /// <summary>
        ///销售 单价
        /// </summary>
        public decimal Price { get; set; }

        /// <summary>
        /// 库存，t
        /// </summary>
        [DecimalPrecision(18, 3)]
        public decimal Amount { get; set; }

        /// <summary>
        /// 商品数量,只用于盘库，手动填入
        /// </summary>
        public int Count { get; set; }

        /// <summary>
        /// 最小库存数量，负库存
        /// </summary>
        public int MinCount { get; set; }

        /// <summary>
        /// 最小库存重量，负库存
        /// </summary>
        [DecimalPrecision(18, 3)]
        public decimal MinAmount { get; set; }

        /// <summary>
        /// 锁定库存
        /// </summary>
        [DecimalPrecision(18, 3)]
        public decimal LockAmount { get; set; }

        /// <summary>
        /// 锁定数量
        /// </summary>
        public int LockCount { get; set; }
        
        /// <summary>
        /// 价格库存最后更新时间
        /// </summary>
        public DateTime UpdateTime { get; set; }

        #region IDbEntity 成员

        public object[] GetKeyValues()
        {
            return new object[] { ProviderId, CommodityId };
        }

        public void SetKeyValues(object[] keyValues)
        {
            ProviderId = (Guid)keyValues[0];
            CommodityId = (Guid)keyValues[1];

        }

        #endregion

        public class Mapper : BaseMapToCfg<DepotCommodity>
        {
            public Mapper()
            {
                ToTable("sal_DepotCommodity");
                HasKey(p => new { p.ProviderId, p.CommodityId });
                HasRequired(p => p.Provider).WithMany(p => p.DepotCommoditys);
                HasRequired(p => p.Commodity);

            }
        }
        /// <summary>
        /// 验证库存是否够
        /// </summary>
        /// <param name="amount">库存数量</param>
        /// <returns></returns>
        public bool CheckAmount(decimal weight)
        {
            return Amount - weight >= -MinAmount;
        }
    }

    /// <summary>
    /// 仓库商品历史记录
    /// </summary>
    public class DepotCommodityRecord : DbEntityBase<Guid>
    {
        /// <summary>
        /// 供应商
        /// </summary>
        public virtual Provider Provider { get; set; }

        /// <summary>
        /// 仓库编号
        /// </summary>
        public Guid DepotId { get; set; }

        /// <summary>
        /// 商品
        /// </summary>
        public virtual Commodity Commodity { get; set; }

        /// <summary>
        /// 商品编号
        /// </summary>
        public Guid CommodityId { get; set; }

        /// <summary>
        /// 单价
        /// </summary>
        public decimal Price { get; set; }

        /// <summary>
        /// 库存，t
        /// </summary>
        [DecimalPrecision(18, 3)]
        public decimal Amount { get; set; }

        /// <summary>
        /// 价格库存更新时间
        /// </summary>
        public DateTime UpdateTime { get; set; }

        public class Mapper : SalMapper<DepotCommodityRecord, Guid>
        {
            public Mapper()
            {
            }
        }
        /// <summary>
        /// 仓库商品历史转仓库商品实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public DepotCommodity ToDepotCommodity(DepotCommodity entity = null)
        {
            if (entity == null)
            {
                entity = new DepotCommodity();
            }
            entity.Provider = this.Provider;
            entity.Commodity = this.Commodity;
            entity.Price = this.Price;
            entity.Amount = this.Amount;
            entity.UpdateTime = this.UpdateTime;
            return entity;
        }
        /// <summary>
        /// 仓库商品历史转仓库商品实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static DepotCommodityRecord ToDepotCommodityRecord(DepotCommodity dcmmd)
        {
          var entity = new DepotCommodityRecord();

          entity.Provider = dcmmd.Provider;
          entity.Commodity = dcmmd.Commodity;
          entity.DepotId = dcmmd.ProviderId;
          entity.CommodityId = dcmmd.CommodityId;
          entity.Price = dcmmd.Price;
          entity.Amount = dcmmd.Amount;
          entity.UpdateTime = dcmmd.UpdateTime;
            return entity;
        }
    }
}

